package com.flyco.modules.oms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.flyco.common.constant.CommonConstant;
import com.flyco.common.util.HttpUtil;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.oms.entity.OmsOrderAddition;
import com.flyco.modules.oms.mapper.OmsOrderAdditionMapper;
import com.flyco.modules.oms.service.IOmsOrderAdditionService;
import com.flyco.modules.oms.vo.OmsOrderVO;
import com.flyco.modules.system.entity.SysOpenAccount;
import com.flyco.modules.system.service.ISysOpenAccountService;
import com.flyco.modules.util.ApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Objects;

/**
 * @Description: 订单附加
 * @Author: flyco
 * @Date:   2021-07-27
 * @Version: V1.0
 */
@Slf4j
@Service
public class OmsOrderAdditionServiceImpl extends ServiceImpl<OmsOrderAdditionMapper, OmsOrderAddition> implements IOmsOrderAdditionService {

    @Autowired
    private OmsOrderAdditionMapper omsOrderAdditionMapper;
    @Autowired
    private ISysOpenAccountService sysOpenAccountService;


    @Override
    public void afterSign(String id) {
        OmsOrderVO vo = omsOrderAdditionMapper.getOrderAdditionById(id);
        if (oConvertUtils.isNotEmpty(vo)
                && oConvertUtils.isNotEmpty(vo.getPartnerId())
                && oConvertUtils.isNotEmpty(vo.getScene())
                && oConvertUtils.isNotEmpty(vo.getOpenId())
                && Objects.equals(vo.getScene(), "minishop")) {
            SysOpenAccount openAccount = sysOpenAccountService.getAccountByPartnerAndBizCode(vo.getPartnerId(), CommonConstant.OPEN_ACCOUNT_BIZ_CODE_MINIAPP, CommonConstant.OPEN_ACCOUNT_SOURCE_WECHAT);
            if (oConvertUtils.isNotEmpty(openAccount)) {
                StringBuilder url = new StringBuilder();
                url.append(ApiUtil.getWxmaUrl());
                url.append("/wx/miniapp/shop/delivery/");
                url.append(openAccount.getClientKey());
                //url.append("wx5003af1ba4f23e32");
                url.append("/recieve");
                log.info("url: {}",url);
                JSONObject params = new JSONObject();
                params.put("id",id);
                params.put("openid",vo.getOpenId());

                String result = HttpUtil.postRequest(url.toString(), params );
                log.info("result: {}",result);
            }

        }
    }
}
